package com.jackson0714.common.exception;

import com.jackson0714.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
  * @Description:全局异常捕获
  * @Version v1.0.0
  * @author zhangpeng148
  * @date 2022/1/21 11:55
  */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /** 
     * 捕获业务处理异常
     * @param: [e] 
     * @return: com.jackson0714.common.utils.R 
     */
    @ExceptionHandler(BusinessException.class)
    public R handlerBusinessException(BusinessException e) {
        log.error("全局捕获业务异常:{}",e.getbMessage());
        return R.error(e.getbCode(), e.getbMessage());
    }

    /** 
     * 捕获其他异常
     *
     * @param: [e] 
     * @return: com.jackson0714.common.utils.R 
     */
    @ExceptionHandler(Exception.class)
    public R handlerException(Exception e, HttpServletRequest request ,HttpServletResponse response) {
        String requestURI = request.getRequestURI();
        log.error("请求路径:{},全局捕获异常:{}",requestURI,e.getMessage());
        return R.error(e.getMessage());
    }
}


